<?php
include_once("../module/micro_shop/includes/plugin_coupons_class.php");
$couponsObj = new Coupons();

$error = "";
if($_POST['act'] && $_POST['act']=='create'){
    if($_POST['member_id']!="" && $_POST['configId']>0){
        $member = explode(',', str_replace("，", ",", $_POST['member_id']));
        $donothaveshopmemids = "";
        foreach ($member as $memberId)
        {
            $sql = "select member_id from ".MICROSHOP." where member_id={$memberId}";
            $db->query($sql);
            $shop = $db->fetchRow();
            if(empty($shop)){
                $donothaveshopmemids .= $donothaveshopmemids?",".$memberId:$memberId;
                continue;
            }
            $couponsObj->createOnePackage($memberId, $_POST['configId']);
        }
        die(json_encode(array('code'=>0,'msg'=>'成功'.($donothaveshopmemids?"未开店用户不可生成：".$donothaveshopmemids:""))));
    }else{
        die(json_encode(array('code'=>'-1','msg'=>'未填写用户ID或未选择礼包配置！')));
    }
}

//查询礼包配置
$sql = "select id,name,config_json from ".MICROSHOPCOUPONSCONFIG." where status=1";
$db->query($sql);
$configArr = $db->getRows();

$tpl->assign('error',$error);
$tpl->assign('configArr',$configArr);
$tpl->display('coupons_package_create.htm');